sap.ui.define([
		"sap/ui/base/ManagedObject",
		"sap/ui/core/Fragment",
		"sap/ui/core/syncStyleClass"
	],
	function(ManagedObject, Fragment, syncStyleClass) {
		"use strict";

		return ManagedObject.extend("com.wp.SWIFTSAP.controller.SendbackDialog", {
			constructor: function(oView, oBtn, oThis) { //Othis is detail controller
				this._oView = oView;
				this._oBtn = oBtn;
				this._oThis = oThis;
			},
			exit: function() {
				delete this._oView;
			},
			onOkDialog: function() {
				var odetailThis = this._oThis;
				var oModel = odetailThis.getView().getModel();
				var Requestid = odetailThis.vRequestid; //oThis.getView().byId("requestid").getText();
				var Approverid = odetailThis.getOwnerComponent().getModel("user").getProperty("/Userid");
				var Language = odetailThis.getOwnerComponent().getModel("user").getProperty("/Language");

				var Comments = odetailThis.getView().byId("comments").getValue();
				var Applevel = odetailThis.vApplevel;
				var Sendbacklevel = "";

				var sendbackJSON = odetailThis.getModel("detailView").getProperty("/sendbackjson");
				for (var i in sendbackJSON) {
					if (sendbackJSON[i].Selected) {
						Sendbacklevel = sendbackJSON[i].Applevel;
						break;
					}
				}

				var fSuccessCallback = function() {
					odetailThis.onCloseDetailPress();
				};

				oModel.callFunction(
					"/ProcessRequest", {
						method: "POST",
						urlParameters: {
							Requestid: Requestid,
							Approverid: Approverid,
							Applevel: Applevel,
							Language: Language,
							Sendbacklevel: Sendbacklevel,
							Action: "SENDBACK",
							Comments: Comments,
							Forwardapprovers:""
						},
						success: function(oData) {
							this._oBtn.setEnabled(true);
							if (oData.Type === "S") {
								this._oBtn.setEnabled(true);
								this._oView.byId("sendbackDialog").close();
								odetailThis.showSuccessMessage(odetailThis.getResourceBundle().getText("processRequestSuccess"), fSuccessCallback);
							} else {
								odetailThis.showErrorMessage(odetailThis.getResourceBundle().getText("processRequestFail", [oData.Message]));
							}
						}.bind(this),
						error: function(oError) {
							this._oBtn.setEnabled(true);
							odetailThis.showErrorMessage(oError.responseText);
						}.bind(this)
					});
			},

			open: function() {
				var oView = this._oView;
				//如果没有相应Dialog，就调用sap.ui.xmlfragment实例化Fragment
				if (!oView.byId("sendbackDialog")) {
					// load asynchronous XML fragment
					Fragment.load({
						id: oView.getId(),
						name: "com.wp.SWIFTSAP.view.Sendback",
						controller: this
					}).then(function(oDialog) {
						// connect dialog to the root view of this component (models, lifecycle)
						//该Dialog依赖于oView, oView销毁它也销毁了
						oView.addDependent(oDialog);
						// forward compact/cozy style into dialog
						syncStyleClass(oView.getController().getOwnerComponent().getContentDensityClass(), oView, oDialog);
						oDialog.setResizable(true);
						oDialog.open();
					});
				} else {
					oView.byId("sendbackDialog").open();
				}
			},
			onCancelDialog: function() {
				this._oBtn.setEnabled(true);
				this._oView.byId("sendbackDialog").close();
			}
		});
	});